package cn.iocoder.yudao.module.book.dal.mysql.users;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.book.controller.admin.users.vo.userDTOPageReqVO;
import cn.iocoder.yudao.module.book.dal.dataobject.users.userDTODO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 用户 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface userDTOMapper extends BaseMapperX<userDTODO> {

    default PageResult<userDTODO> selectPage(userDTOPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<userDTODO>()
                .eqIfPresent(userDTODO::getUserId, reqVO.getUserId())
                .eqIfPresent(userDTODO::getOpenid, reqVO.getOpenid())
                .likeIfPresent(userDTODO::getNickname, reqVO.getNickname())
                .eqIfPresent(userDTODO::getAvatarUrl, reqVO.getAvatarUrl())
                .eqIfPresent(userDTODO::getIsMember, reqVO.getIsMember())
                .eqIfPresent(userDTODO::getMobile, reqVO.getMobile())
                .betweenIfPresent(userDTODO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(userDTODO::getUserId));
    }

    default userDTODO selectByMobile(@Param("mobile") String mobile) {
        return selectOne(userDTODO::getMobile, mobile);
    }

    Long selectByUsername(@Param("username") String username);

    userDTODO selectByOpenId(String openid);

    /**
     * 物理删除用户相关的打卡记录
     */
    @Select("DELETE FROM clock_in WHERE user_id = #{userId}")
    void deleteUserClockIns(@Param("userId") Long userId);

    /**
     * 物理删除用户相关的音频记录
     */
    @Select("DELETE FROM audio WHERE user_id = #{userId}")
    void deleteUserAudios(@Param("userId") Long userId);

    /**
     * 物理删除用户相关的书架记录
     */
    @Select("DELETE FROM bookshelf WHERE user_id = #{userId}")
    void deleteUserBookshelves(@Param("userId") Long userId);

    /**
     * 物理删除用户相关的阅读记录
     */
    @Select("DELETE FROM reading_records WHERE user_id = #{userId}")
    void deleteUserReadingRecords(@Param("userId") Long userId);

    /**
     * 物理删除用户相关的收藏记录
     */
    @Select("DELETE FROM user_collect WHERE user_id = #{userId}")
    void deleteUserCollects(@Param("userId") Long userId);

    /**
     * 物理删除用户相关的班级关联
     */
    @Select("DELETE FROM user_classes WHERE user_id = #{userId}")
    void deleteUserClasses(@Param("userId") Long userId);

    /**
     * 物理删除用户基本信息
     */
    @Select("DELETE FROM users WHERE user_id = #{userId}")
    void physicalDeleteUser(@Param("userId") Long userId);

}